package com.tencent.elife.asyn;

/**
 * @Description一个异步任务，保存了任务完成后的回调对象response, 耗时操作在工作线程中执行
 * @author zijianlu
 */
public class Command<T> extends CancelableTask implements Runnable {

    private final BaseManager mManager;
    private final DataResponse<T> mResponse;
    private final DataRequest<T> mRequest;

    public Command(DataRequest<T> request, DataResponse<T> response,
        BaseManager manager) {
        mRequest = request;
        mResponse = response;
        mManager = manager;
    }

    /** 不要重写此函数 */
    public void run() {
        try {
            mResponse.result = mRequest.doInBackGround();
            if (!isCanceled) {
                mManager.onFinish(this);
            }
        } catch (Exception e) {
            if (!isCanceled) {
                mManager.onError(e, this);
            }
        }
    }

    /** 获取响应对象 */
    public DataResponse<T> getResponse() {
        return mResponse;
    }
}
